package view;

import pojo.House;
import service.HouseService;
import utils.Utility;

import java.io.IOException;
import java.util.List;

/**
 * 界面类
 * 1. 显示界面
 * 2. 接受用户的输入
 * 3. 调用HouseService完成对房屋信息的各种操作
 */

public class HouseView {
    private  Boolean loop = true;
    private char key = ' ';
    private HouseService houseService= new HouseService();

    public void menu() throws IOException {
        do{
            System.out.println("----------房屋出租系统----------");
            System.out.println("\t\t1 新 增 房 源");
            System.out.println("\t\t2 查 找 房 屋");
            System.out.println("\t\t3 删 除 房 屋");
            System.out.println("\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t5 房 屋 列 表");
            System.out.println("\t\t6 退      出");
            System.out.printf("请选择(1-6)：");
            key = Utility.readChar();
            switch(key){
                case '1':
                    addHouse();
                    break;
                case '2':
                    selectHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    reviseHouse();
                    break;
                case '5':
                    show();
                    break;
                case '6':
                    exit();
                    break;
            }
        }while(loop);
    }

    public void addHouse() throws IOException {
        System.out.println("--------------新增房屋--------------");
        System.out.printf("姓名：");
        String HomeOwnerName = Utility.readString(5);
        System.out.printf("电话：");
        String HomeOwnerNumber = Utility.readString(11);
        System.out.printf("地址：");
        String HomeOwnerAdder = Utility.readString(16);
        System.out.printf("月租：");
        int HOmonthlyRent = Utility.readInt(6);
        System.out.printf("状态(未出租/已出租)：");
        String state = Utility.readString(4);
        House newHouse = new House(0,HomeOwnerName,HomeOwnerNumber,HomeOwnerAdder, HOmonthlyRent,state);
        if (houseService.add(newHouse)){
            System.out.println("--------------添加完成--------------");
        }else{
            System.out.println("--------------添加失败--------------");
        }
    }
    public void show(){
        int i = 0;
        System.out.println("=============房屋列表============");
        System.out.println("编号\t\t房主\t\t电话\t\t\t地址\t\t\t\t\t\t月租\t\t状态(未出租/已出租)");
        List<House> list =  houseService.list();
        for (House house:list
             ) {
            System.out.println(house);
        }

        System.out.println("=============房屋列表显示完毕============");
    }
    public void selectHouse(){
        System.out.println("=============查找房屋============");
        System.out.printf("请输入你要查找的id：");
        int sel = Utility.readInt();
        House house = houseService.select(sel);
        if (house !=null){
            System.out.println("编号\t\t房主\t\t电话\t\t\t地址\t\t\t\t\t\t月租\t\t状态(未出租/已出租)");
            System.out.println(house);
        }else{
            System.out.println("查询不到该ID");
        }

    }
    public void delHouse(){
        System.out.println("==========删除房屋=========");
        System.out.print("请选择待删除的房屋编号（输入-1退出）：");
        int del = Utility.readInt();
        House house = houseService.select(del);
        if (house != null && del != -1){
            System.out.println("确认是否删除(Y/N)：");
            String is = Utility.readString(1);
            if (is.equals("Y")){
                if (houseService.delete(del)){
                    System.out.println("删除成功！");
                }else{
                    System.out.println("删除失败，编号查询不到");
                }
            }else{
                System.out.println("放弃删除,返回上一步");
            }
        }else{
            System.out.println("查询不到改id");
        }


    }
    public void reviseHouse(){
        System.out.println("==========修改房屋==========");
        System.out.printf("请选择待修改房屋编号(-1退出)");
        int sel = Utility.readInt();
        House house = houseService.select(sel);
        if (house !=null && sel != -1){
            System.out.printf("姓名(%s)：",house.getName());
            String HomeOwnerName = Utility.readString(5);
            System.out.printf("电话(%s)：",house.getNumber());
            String HomeOwnerNumber = Utility.readString(11);
            System.out.printf("地址(%s)：",house.getAddress());
            String HomeOwnerAdder = Utility.readString(16);
            System.out.printf("月租(%d)：",house.getRent());
            int HOmonthlyRent = Utility.readInt(6);
            System.out.printf("状态(%s)：",house.getStatus());
            String states = Utility.readString(4);
            House newHouse = new House(sel,HomeOwnerName,HomeOwnerNumber,HomeOwnerAdder, HOmonthlyRent,states);
            if(houseService.revise(newHouse)){
                System.out.println("修改成功！");
            }else{
                System.out.println("修改失败！");
            }
        }else{
            System.out.println("查询不到该ID");
        }
    }
    //完成退出确认
    public void exit() {
        //这里我们使用Utility提供方法，完成确认
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            loop = false;
        }
    }

}
